package ae.pcfc.cldos.online.common.etksdb.dao.provider;

import java.util.List;

import org.hibernate.FetchMode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import ae.pcfc.cldos.online.common.dao.GenericDAO;
import ae.pcfc.cldos.online.common.etksdb.dao.UserDAO;
import ae.pcfc.cldos.online.common.etksdb.domain.Client;
import ae.pcfc.cldos.online.common.etksdb.domain.User;

/**
 * 
 * @author arosa
 *
 */
public class UserDAOImpl extends GenericDAO implements UserDAO {

	private static final String CLIENT_ALIAS = "clientAlias";
	
	public User findUserByUserName(String strUserName) {
		
		DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
		criteria.add(Restrictions.eq(User.__USERNAME, strUserName).ignoreCase());
		
		return (User) findByCriteria(criteria);
	}
	
	public User findUserAndClientByUserName(String strUserName) {
		
		DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
		criteria.add(Restrictions.eq(User.__USERNAME, strUserName).ignoreCase());
		criteria.createAlias(User.__CLIENT, CLIENT_ALIAS);
		criteria.setFetchMode(CLIENT_ALIAS, FetchMode.JOIN);
		
		return (User) findByCriteria(criteria);
	}

	@SuppressWarnings("unchecked")
	public List<User> getUserList() {
		DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
		
		return (List<User>)getListByCriteria(criteria);
	}

	public User finByPrimaryKey(Long id) {
		return findById(User.class, id);
		
	}
	
	public String getTerakheesIdByUsername(String username){
		DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
		criteria.add(Restrictions.eq(User.__USERNAME, username));
		criteria.setFetchMode(User.__CLIENT, FetchMode.JOIN);
		User user = findByCriteria(criteria);
		String result = null;
		if(user!=null){
			Client client = user.getClient();
			if(client!=null){
				result = client.getCTrakheesId();
			}
		}
		return result;
	}

//	public User getUserByEmail(String email) {
//		
//		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(User.class, "user");
//		criteria.add(Restrictions.like("user.UEmail", email).ignoreCase());
//		List<User> users = criteria.list();
//		
//		return (users != null && users.size() > 0 ) ? users.get(0) : null;
//	}

	public void add(User user) {
		saveOrUpdate(user);
	}

	public void update(User user) {
		saveOrUpdate(user);
	}
	
	@SuppressWarnings("unchecked")
	private List<String> getUsersEmailsList(Long clientId, String userType){
		DetachedCriteria criteria =  DetachedCriteria.forClass(User.class);
		if(userType!=null){
			criteria.add(Restrictions.eq(User.USER_TYPE, userType));
		}
		criteria.add(Restrictions.eq(User.CLIENT_ID, clientId));
		criteria.setProjection(Projections.property(User.EMAIL));
		return (List<String>)getListByCriteria(criteria);
	}
	
	public List<String> getSubUsersEmailsListByCliendId(Long clientId){
		return getUsersEmailsList(clientId, User.USER_TYPE_SUB);
	}
	
	public List<String> getMainUsersEmailsListByCliendId(Long clientId){
		return getUsersEmailsList(clientId, User.USER_TYPE_MAIN);
	}
	
	public List<String> getUsersEmailsListByCliendId(Long clientId){
		return getUsersEmailsList(clientId, null);
	}
	
	public String getUserEmailById(Long userId){
		DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
		criteria.add(Restrictions.idEq(userId));
		criteria.setProjection(Projections.property(User.EMAIL));
		return (String)getListByCriteria(criteria).get(0);
	}
	

}
